package iOperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author: Lenovo
 * @Date: 2024/5/24 19:25
 * @description:
 */
public class AddOperation implements IOPeration{
    public void work(BookList bookList){
        //1.判满和判空
        System.out.println("新增图书...");
        int currentSize= bookList.getUseSize();
        if(currentSize==0){
            System.out.println("书架中已无书");
            return;
        }
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架已放满书");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名:");
        String name=scanner.nextLine();
        System.out.println("请输入作者:");
        String author=scanner.nextLine();
        System.out.println("请输入类型:");
        String type=scanner.nextLine();
        System.out.println("请输入价格:");
        Double price= scanner.nextDouble();
        Book newbook=new Book(name,author,price,type);
        for (int i = 0; i < currentSize; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("此书在书架上已存在...");
                return;
            }
        }
        //插入书籍
        bookList.setUseSize(currentSize+1);
        bookList.setBook(currentSize,newbook);
        System.out.println("新书增加成功...");
    }
}
